/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mzm.ui.admin;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;

import com.mzm.api.MediaInfoDto;
import com.mzm.api.ProductInfoDto;
import com.mzm.api.RecommendationDto;
import com.mzm.remoting.IMzmServices;
import com.mzm.ui.BaseApp;

import dnl.util.ui.WindowUtils;
import dnl.util.ui.actions.AbstractAction;

/**
 * 
 * @author daniel
 */
public class TaskOrientedAdminApp extends BaseApp {

	private AdminContext adminContext;

	private NewCategoryDialog newCategoryDialog = new NewCategoryDialog(this, true);
	private ProductInfoEditingDialog productInfoEditingDialog = new ProductInfoEditingDialog(true, this, true);
	private CategorySelectionDialog categorySelectionDialog = new CategorySelectionDialog(this, true);
	private RecommendationEditingDialog recommendationEditingDialog = new RecommendationEditingDialog(this,
			true);

	private RecommendationManagementDialog recommendationManagementDialog = new RecommendationManagementDialog(
			this, true);
	private MediaInfoEditingDialog mediaInfoEditingDialog = new MediaInfoEditingDialog(this, true);

	/**
	 * Creates new form MzmAdminApp
	 */
	public TaskOrientedAdminApp() {
		super("MZM Admin");
		initComponents();
		adminContext = new AdminContext();
		recommendationEditingDialog.setAdminContext(adminContext);
		JXTaskPaneContainer tpc = new JXTaskPaneContainer();
		JXTaskPane categoryGroup = new JXTaskPane();
		categoryGroup.setName("categoryGroup");
		JXTaskPane mediaGroup = new JXTaskPane();
		mediaGroup.setName("mediaGroup");
		JXTaskPane recommendationGroup = new JXTaskPane();
		recommendationGroup.setName("recommendationGroup");
		tpc.add(categoryGroup);
		tpc.add(mediaGroup);
		tpc.add(recommendationGroup);
		registerCategoryActions(categoryGroup);
		registerRecommendationActions(recommendationGroup);
		registerMediaActions(mediaGroup);
		getContentPane().add(tpc, BorderLayout.WEST);
	}

	private void registerCategoryActions(JXTaskPane categoryGroup) {
		categoryGroup.add(new AbstractAction("New Category") {
			@Override
			public void actionPerformedImpl(ActionEvent e) {
				if (!assertConnected()) {
					return;
				}
				newCategoryDialog.setParentsList(adminContext.getMzmAdminFacade().getAllCategoriesAsList());
				WindowUtils.setWindowSize(newCategoryDialog, .5, .4);
				WindowUtils.centerWindowRelative(newCategoryDialog, TaskOrientedAdminApp.this);
				newCategoryDialog.clear();
				newCategoryDialog.setVisible(true);
				if (!newCategoryDialog.wasCancelled()) {
					String parentCategory = newCategoryDialog.getParentCategory();
					String categoryName = newCategoryDialog.getCategoryName();
					String fullName = parentCategory.equals("/") ? "/" + categoryName : parentCategory + "/"
							+ categoryName;
					adminContext.getMzmAdminFacade().createCategory(fullName);
				}
			}
		});
		categoryGroup.add(new AbstractAction("New Product") {
			@Override
			public void actionPerformedImpl(ActionEvent e) {
				if (!assertConnected()) {
					return;
				}
				// this dialog may have other titles as well
				productInfoEditingDialog.setTitle("New Product");
				productInfoEditingDialog.clear();
				productInfoEditingDialog.setCategoryList(adminContext.getMzmAdminFacade()
						.getAllCategoriesAsList());
				WindowUtils.setWindowSize(productInfoEditingDialog, .5, .5);
				WindowUtils.centerWindow(productInfoEditingDialog);
				productInfoEditingDialog.setVisible(true);
				if (!productInfoEditingDialog.wasCancelled()) {
					ProductInfoDto productInfoDto = productInfoEditingDialog.getProductInfoDto();
					adminContext.getMzmAdminFacade().addProductInfo(
							productInfoEditingDialog.getSelectedCategory(), productInfoDto);
					adminContext.refreshProductList();
				}
			}
		});
		categoryGroup.add(new AbstractAction("Delete Category") {
			@Override
			public void actionPerformedImpl(ActionEvent e) {
				if (!assertConnected()) {
					return;
				}
				categorySelectionDialog.setParentsList(adminContext.getMzmAdminFacade()
						.getAllCategoriesAsList());
				WindowUtils.setWindowSize(categorySelectionDialog, .5, .4);
				WindowUtils.centerWindowRelative(categorySelectionDialog, TaskOrientedAdminApp.this);
				categorySelectionDialog.setVisible(true);
				if (!categorySelectionDialog.wasCancelled()) {
					String cat = categorySelectionDialog.getParentCategory();
					adminContext.getMzmAdminFacade().deleteCategory(cat);
				}
			}
		});
	}

	private boolean assertConnected() {
		if (adminContext.getMzmAdminFacade() == null) {
			JOptionPane.showMessageDialog(TaskOrientedAdminApp.this, "Not connected");
			return false;
		}
		return true;
	}

	private void registerRecommendationActions(JXTaskPane recommendationGroup) {
		recommendationGroup.add(new AbstractAction("New Recommendation") {
			@Override
			public void actionPerformedImpl(ActionEvent e) {
				recommendationEditingDialog.setTitle("New Recommendation");
				recommendationEditingDialog.refresh();
				WindowUtils.setWindowSize(recommendationEditingDialog, .65, .7);
				WindowUtils.centerWindowRelative(recommendationEditingDialog, TaskOrientedAdminApp.this);
				recommendationEditingDialog.setVisible(true);
				if (!recommendationEditingDialog.wasCancelled()) {
					RecommendationDto recommendation = recommendationEditingDialog.getRecommendationDto();
					adminContext.getMzmAdminFacade().addRecommendation(
							recommendationEditingDialog.getSelectedProduct().getId(), recommendation);
				}
			}
		});
		recommendationGroup.add(new AbstractAction("Manage Recommendations") {

			@Override
			public void actionPerformedImpl(ActionEvent e) {
				WindowUtils.setWindowSize(recommendationManagementDialog, .9, .8);
				WindowUtils.centerWindowRelative(recommendationManagementDialog, TaskOrientedAdminApp.this);
				recommendationManagementDialog.pack();
				recommendationManagementDialog.setVisible(true);
			}
		});
	}

	private void registerMediaActions(JXTaskPane mediaGroup) {
		mediaGroup.add(new AbstractAction("New Media") {
			@Override
			public void actionPerformedImpl(ActionEvent e) {// this dialog may
															// have other titles
															// as well
				mediaInfoEditingDialog.setTitle("New Media");
				mediaInfoEditingDialog.clear();
				WindowUtils.setWindowSize(productInfoEditingDialog, .5, .5);
				WindowUtils.centerWindow(productInfoEditingDialog);
				mediaInfoEditingDialog.setVisible(true);
				if (!mediaInfoEditingDialog.wasCancelled()) {
					MediaInfoDto mediaInfo = mediaInfoEditingDialog.getMediaInfoDto();
					adminContext.getMzmAdminFacade().addMediaInfo(mediaInfo);
					adminContext.refreshProductList();
				}
			}
		});
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(TaskOrientedAdminApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(TaskOrientedAdminApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(TaskOrientedAdminApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(TaskOrientedAdminApp.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				TaskOrientedAdminApp app = new TaskOrientedAdminApp();
				WindowUtils.setWindowSize(app, .9, .9);
				WindowUtils.centerWindow(app);
				app.setVisible(true);
				app.connectTo("localhost", "8080");
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	// End of variables declaration//GEN-END:variables

	@Override
	protected void connected() {
		IMzmServices services = getBizAppServices();
		adminContext.setMzmAdminFacade(services.getMzmAdminFacade());
		recommendationManagementDialog.setAdminContext(adminContext);
	}
}
